/*
 * xAPBSCendpoint.java
 *
 * Java class used to store xAP BSC endpoint data
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 12/08/09 by Daniel Berenguer : first version
 */

package xAP;

/********************************************************************************************
 * xAPBSCendpoint class
 *
 * Class used to contain xAP BSC endpoint data
 *
 *********************************************************************************************/
public class xAPBSCendpoint
{
   /********************************************************************************************
    * Member variables
    ********************************************************************************************/
    // xAP header fields
    public String uid = "";         // xAP UID
    public String source = "";      // xAP source
    // Body fields
    public xAPBSCstate state;       // ON/OFF BSC state
    public xAPBSCtext text;         // BSC text value
    public xAPBSClevel level;       // BSC level

    // Other data
    protected String hbody;         // Body header (tittle)

   /********************************************************************************************
    * xAPBSCendpoint
    *
    * Class constructor
    *
    * 'message'     xAP BSC event or info message to extract the endpoint data from
    *
    ********************************************************************************************/ 
    public xAPBSCendpoint(xAPBSCmessage message)
    {
        uid = message.uid;
        source = message.source;
        state = message.state;
        text = message.text;
        level = message.level;
        hbody = message.hbody;
    }

    public xAPBSCendpoint()
    {
        
    }

   /********************************************************************************************
    * isInfo
    *
    * Check if the xAP class passed as argument has associated information in the message body
    * Only xAPBSC events and info messages will return true
    *
    * 'xClass'          xAP class
    *
    *  Return:
    *   true if the message is a BSC event or info
    *   false otherwise
    ********************************************************************************************/
    public boolean isInfo(String xClass)
    {
        String strAllowedClass[] = {"xAPBSC.event", "xAPBSC.info"};
        int i;

        for(i=0 ; i<strAllowedClass.length; i++)
        {
            if (xClass.compareToIgnoreCase(strAllowedClass[i]) == 0)
                return true;
        }

        return false;
    }

   /********************************************************************************************
    * getDeviceUID
    *
    * Get device UID from endpoint UID
    *
    *  Return:
    *   true if the message is a BSC event or info
    *   false otherwise
    ********************************************************************************************/
    public String getDeviceUID()
    {
        int intPos = this.uid.lastIndexOf(':');

        // For xAP version = 12
        if (intPos == -1)
            return this.uid.substring(6);
        // For xAP version = 13
        else
            return this.uid.substring(intPos);
    }

   /********************************************************************************************
    * getDeviceSource
    *
    * Get device Source from endpoint Source
    *
    *  Return:
    *   device source address string
    ********************************************************************************************/
    public String getDeviceSource()
    {
        int intPos = this.source.indexOf(':');

        if (intPos == -1)
            return null;

        return this.source.substring(0, intPos);
    }

   /********************************************************************************************
    * getSubSource
    *
    * Get the source subaddress of the endpoint
    *
    *  Return:
    *   subaddress string
    ********************************************************************************************/
    public String getSubSource()
    {
        int intPos = this.source.indexOf(':');

        if (intPos == -1)
            return null;

        return this.source.substring(intPos+1);
    }

   /********************************************************************************************
    * isOutput
    *
    * Return true if the BSC endpoint is an output
    *
    *  Return:
    *   true if the BSC endpoint is an output
    *   false if it's an input
    ********************************************************************************************/
    public boolean isOutput()
    {
        String title = hbody.toLowerCase();

        if (title.indexOf("output") != -1)
            return true;
        else
            return false;
    }

   /********************************************************************************************
    * belongsToDevice
    *
    * Return true if the BSC endpoint belongs to the device passed as ergument
    *
    * 'device'  device to check against the endpoint
    *
    *  Return:
    *   true if the BSC endpoint belongs to the device
    *   false otherwise
    ********************************************************************************************/
    public boolean belongsToDevice(xAPdevice device)
    {
        if (this.uid.indexOf(device.uid) != -1)
            return true;
        else
            return false;
    }

   /********************************************************************************************
    * sendCommand
    *
    * Send xAP BSC command message
    *
    * 'value' new xAP BSC value
    *
    ********************************************************************************************/
    public void sendCommand(Object value)
    {
        xAPBSCcommand bscCommand = new xAPBSCcommand(this.source, value);
        bscCommand.send();
    }
}
